<?php

/**
 * systemList actions.
 *
 * @package    travide
 * @subpackage systemList
 * @author     Travide Project Group
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class systemListActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $this->rootDomainCollection = null;
        $this->listMode = $request->getParameter('list_mode');



        if ($this->listMode == 'full') {
            $this->pageTitle = 'Information Domains with All Current Respective Systems';
            $this->domainSystemSearchKey = 'app_domains_current_system_relation_types';
            $this->systemPropertySearchKey = 'app_list_display_system_properties';
        } else if ($this->listMode == 'planned') {
            $this->pageTitle = 'Information Domains with All Planned Authoritative Systems';
            $this->domainSystemSearchKey = 'app_domains_planned_system_relation_types';
            $this->systemPropertySearchKey = 'app_list_planned_display_system_properties';
        } else {
            //set the default list type
            $this->listMode == 'authoritative';
            $this->pageTitle = 'Information Domains with Current Authoritative Systems Only';
            $this->domainSystemSearchKey = 'app_domains_authoritative_system_relation_types';
            $this->systemPropertySearchKey = 'app_list_display_system_properties';
        }
        /**
         * Find the root Authoritative Systems Collection Item
         *
         * Its relations will form the authoritative systems list
         */
        $this->rootDomainCollection = Doctrine::getTable('CollectionItem')->findOneBy('uin', sfConfig::get('app_general_root_collection_item_uin'));
        if ($this->rootDomainCollection) {
            /*
             * Hunt through the related items by relation type
             * depending on the list mode
             */
            $this->domainRelationList = $this->rootDomainCollection->getRelationsByIdentifierArray(sfConfig::get('app_domains_current_domains_relation_types'),false,true);
        }
    }

    public function executeViewDetails(sfWebRequest $request) {
        $this->collection_item = Doctrine::getTable('CollectionItem')->find(array($request->getParameter('uin')));
        $this->forward404Unless($this->collection_item);
        $this->commentForm = new CollectionCommentForm();
    }

    public function executeCreateComment(sfWebRequest $request) {
        $this->collection_item = Doctrine::getTable('CollectionItem')->find(array($request->getParameter('uin')));
        $this->forward404Unless($this->collection_item);
        $this->commentForm = new CollectionCommentForm();

        $collectionComment = $this->processCommentForm($request, $this->commentForm, $this->collection_item->getUin());



        if ($collectionComment != null) {



            $this->redirect('domainViewDetails', array('uin' => $this->collection_item->getUin()));
        } else {
            $this->setTemplate('viewDetails');
        }
    }

    public function executeNewComment(sfWebRequest $request) {
        /**
         * Fetch the collection for the AuthoritativeSystems Collection
         */
        $this->collection_item = Doctrine::getTable('CollectionItem')->find(sfConfig::get('app_travide_link_general_comments_collection_identifier'));
        $this->forward404Unless($this->collection_item);
        $this->commentForm = new CollectionCommentForm();
    }

    protected function processCommentForm(sfWebRequest $request, sfForm $form, $collectionItemUin) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            /**
             * Override the collection Item's uin wit hthe one supplied
             * (since it shouldn't actually be set by the user anyway.
             */
            $form->getObject()->setCollectionUin($collectionItemUin);
            $collectionComment = $form->save();
            return $collectionComment;
        } else {
            return null;
        }
    }

}
